package com.bawei.mdm2207alast.mine.ask

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.common.UserCard
import com.bawei.mdm2207alast.model.db.AskEntity
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * 作者：赵亮
 * 时间：2024/12/24 16:24
 * 功能：审批页面
 */
@Composable
fun AskApprovePage(
    navController: NavController = rememberNavController(),
    json: String = "",
    viewModel: AskViewModel = hiltViewModel()
) {

    val askEntity = Gson().fromJson(json, AskEntity::class.java)

    var approve by remember {
        mutableStateOf("同意")
    }

    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when(it) {
                    is OnError -> {ToastUtils.showShort("操作失败")}
                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        ToastUtils.showShort("审批成功")
                        navController.popBackStack()
                    }
                }
            }
        }
    }

    Column {
        BaseTitle(navController = navController, title = "请假详情")

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(start = 15.dp, end = 15.dp, top = 20.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            // 申报人信息
            Text(text = "申报人信息", color = Color.Blue)
            UserCard(navController = navController, mmkv = MMKV.defaultMMKV())
            // 请假详情
            Spacer(modifier = Modifier.height(20.dp))
            Text(text = "请假详情", color = Color.Blue)
            Text(text = "请假方式：${askEntity.type}")
            Text(text = "开始时间：${askEntity.start}")
            Text(text = "结束时间：${askEntity.end}")
            Text(text = "请假原因：${askEntity.reason}")
            // 审批意见
            Spacer(modifier = Modifier.height(20.dp))
            Text(text = "审批意见", color = Color.Blue)
            Divider()
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "意见内容：")
                RadioButton(selected = approve == "同意", onClick = { approve = "同意" })
                Text(text = "同意")
                RadioButton(selected = approve == "拒绝", onClick = { approve = "拒绝" })
                Text(text = "拒绝")
            }
            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                askEntity.status = approve
                viewModel.sendIntent(AskIntent.UpdateIntent(askEntity))
            }) {
                Text(text = "提交")
            }
        }

    }
}

@Preview
@Composable
fun AskApprovePagePreview() {
    AskApprovePage()
}

